#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int FindLeftBorder(int* nums, int numsSize, int target)
{
    int left = 0;
    int right = numsSize - 1;
    int leftBorder;

    while (left <= right)
    {
        int mid = (right - left) / 2 + left;

        if (nums[mid] >= target)
        {
            right = mid - 1;
            leftBorder = mid;
        }
        else
            left = mid + 1;
    }

    return leftBorder;
}

int FindRightBorder(int* nums, int numsSize, int target)
{
    int left = 0;
    int right = numsSize - 1;
    int rightBorder;

    while (left <= right)
    {
        int mid = (right - left) / 2 + left;

        if (nums[mid] > target)
            right = mid - 1;
        else
        {
            left = mid + 1;
            rightBorder = mid;
        }
    }

    return rightBorder;
}

int search(int* nums, int numsSize, int target) {
    int left = 0;
    int right = numsSize - 1;

    while (left <= right)
    {
        int mid = (right - left) / 2 + left;

        if (nums[mid] > target)
            right = mid - 1;
        else if (nums[mid] < target)
            left = mid + 1;
        else
            break;
    }

    if (right < left)
        return 0;

    return FindRightBorder(nums, numsSize, target) - FindLeftBorder(nums, numsSize, target) + 1;
}

int main()
{
    int nums[] = { 5,7,7,8,8,10 };

    int count = search(nums, sizeof(nums) / sizeof(int), 8);

    return 0;
}